/* ************************************************************************

   Copyright:
     2008 openWorks

   License:
     LGPL: http://www.gnu.org/licenses/lgpl.html
     See the LICENSE file in the project's top-level directory for details.

   Authors:
     * Juan Matias Granda
     * Martín Alejandro Paz

************************************************************************ */

/**
* AGREGAR EN structure selected: true? (existe el metodo setValue que hace esto)<br />
* Objeto creado con un {@link qx.ui.basic.Label} y uno o varios {@link  qx.ui.form.RadioButton}.
* 
* *Ejemplo:*
*
* <pre class='javascript'>
*  var datosEstructura = 
*  [
*   {value: '1' , label: 'nombre del item1'},  
*   {label: 'nombre del item 2', value: '2'},
*   {label: 'nombre del item 4'},
*   {value: '5'}
*  ]
*  var rdbRadios= new qx.ui.form.ow.Radios("Label:",datosEstructura);
*  </pre>
*/
qx.Class.define("qx.ui.form.ow.Radios",
{
  /*
  *****************************************************************************
     EVENTS
  *****************************************************************************
  */
	events:
	{
		/** 
		*  Se dispara cuando un valor del Radio cambia.
		*/
		"changeValue": "qx.event.type.Event"
	},
	extend : qx.ui.container.Composite,
	/**
	* @param label {String} Titulo que acompaña a los Radios.
	* @param  structure {Map} Array de datos que se pasan al Constructor.<br />
	* *Parametros:*
	* <ul>
	* <li>*label:* Texto a mostrar en el item.</li>
	* <li>*value:* Valor del item.</li>
	* </ul>
	* _Aclaracion:_ Al menos debe existir _label_ o _value_, el cual cumplira la funcion de ambos.
	* @param hAlign {Boolean ? false} _true_ si se quiere distribuir los Radios horizontalmente.
	*/
	construct : function (label, structure, hAlign)
	{
		this.base(arguments);

		if (hAlign){
			this.setLayout(new qx.ui.layout.HBox(5));
		}
		else {
			this.setLayout(new qx.ui.layout.VBox(5));
		}

		this._label = new qx.ui.basic.Label(label);
			this._label.setWidth(120);
			this._label.setRich(true);
			this.add(this._label);

		this._rdg = new qx.ui.form.RadioGroup;
		
		this._radios = [];
		
		for ( r in structure){
			if (structure[r].value != null){
				if (structure[r].label != null){
					this._radios.push((new qx.ui.form.RadioButton(structure[r].label)).set({model: structure[r].value}));
				}
				else {
					this._radios.push((new qx.ui.form.RadioButton(structure[r].value)).set({model: structure[r].value}));
				}
			}
			else {
				if (structure[r].label != null){
					this._radios.push((new qx.ui.form.RadioButton(structure[r].label)).set({model: structure[r].label}));
				}
			}
		}

		this._rdg.addListener("changeSelection", function ()
		{
			this.fireEvent('changeValue');
		}, this);

		var r = "";
		for (r in this._radios){
			//*************** EL 'IF' ES PARCHE PARA INTERNET EXPLORER **********************
			if (typeof this._radios[r] != 'function')
			{
				this._rdg.add(this._radios[r]);
				this.add(this._radios[r]);
			}
		}
		
	},
	
/*
*****************************************************************************
     MEMBERS
*****************************************************************************
*/
	members :
	{
		_label : null,
		_radios: null,
		_rdg : null,
		
		/**
		* Devuelve el objeto *qx.ui.basic.Label*.
		* @return {qx.ui.basic.Label}
		*/
		getLabel : function () { return this._label; },
		
		/**
		* Devuelve el valor del *qx.ui.form.RadioButton* seleccionado.
		* @return {String[]}
		*/
		getValue : function () {
			return this._rdg.getSelection()[0].getModel();
		},
		
		/**
		* Devuelve el texto del *qx.ui.basic.Label* del *qx.ui.form.RadioButton* seleccionado.
		* @return {String[]}
		*/		
		getValueDisplay : function () { 
			return this._rdg.getSelected().getLabel();
		},

		/**
		* Asigna que *qx.ui.form.RadioButton* del *qx.ui.form.ow.Radios* sera el seleccionado.
		* @param value {String} Valor del *qx.ui.form.RadioButton* que queremos seleccionar.
		* @return {String[]}
		*/		
		setValue : function (value) {
			this._rdg.setModelSelection([value]);
		},
		focus : function ()
		{
			this._radios[0].focus();
		}
	}
});
